package com.lei.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 使用交换机的消息队列
 * Created by Administrator on 2019/2/26.
 */
@Component
public class S02ExchannelProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //路由key
    private static final String RUOTE_KEY="nmg.statics";
    /**
     * 定义一个exchange交换机
     * @return
     */
    @Bean
    public DirectExchange exchangeStatics(){
        return new DirectExchange("exchange.statics");
    }
    /**
     * 定义一个队列
     * @return
     */
    @Bean
    public Queue nmgQueue(){
        //队列数据默认是持久化的
        return new Queue("nmg.statics.queue");
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(nmgQueue()).to(exchangeStatics()).with(RUOTE_KEY);
    }
    @Scheduled(fixedDelay = 2000L)
    public void send() {
        System.out.println("S02ExchannelProducer");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        String content = format.format(new Date());
        this.rabbitTemplate.convertAndSend("exchange.statics", RUOTE_KEY, "S02ExchannelProducer "+content);
    }
}
